<?php

/*
    [GRIRMS] (C) 2009 Grandison & JM Technologies

    @author kevin<hasson.lai@mobile-grandison.com>
    @version $Id: cache.func.php v2 2010-09-16 kevin $
*/

if(!defined('IN_GRIRMS')) {
	exit('Access Denied');
}

//cache
function cache_exists($file)
{
	if(strpos($file,'.')===false) $file .= '.php';
	$cachefile = APP_CACHEDIR.'./data/'.$file;
	return file_exists($cachefile) ? true : false;
}

function cache_read($file, $mode = 'i')
{
	if(strpos($file,'.')===false) $file .= '.php';
	$cachefile = APP_CACHEDIR.'./data/'.$file;
	if(!file_exists($cachefile)) return array();
	return $mode == 'i' ? include $cachefile : file_get_contents($cachefile);
}

function cache_write($file, $string, $type = 'array')
{
	if(strpos($file,'.')===false) $file .= '.php';
	if(is_array($string))
	{
		$type = strtolower($type);
		if($type == 'array')
		{
			$string = "<?php\n return ".var_export($string,TRUE).";\n?>";
		}
		elseif($type == 'constant')
		{
			$data='';
			foreach($string as $key => $value) $data .= "define('".strtoupper($key)."','".addslashes($value)."');\n";
			$string = "<?php\n".$data."\n?>";
		}
	}
	!is_dir(APP_CACHEDIR.'./data/') && mkdir(APP_CACHEDIR.'./data/');
	$strlen = file_put_contents(APP_CACHEDIR.'./data/'.$file, $string);
	@chmod(APP_CACHEDIR.'./data/'.$file, 0777);
	return $strlen;
}

function cache_delete($file){
	return @unlink(APP_CACHEDIR.'./data/'.$file);
}

?>
